﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="control.aspx.cs" Inherits="Com.CMS263.Web.manage.control" %>

<%@ Import Namespace="Com.CMS263.PO" %>
<%@ Import Namespace="Com.CMS263.Util" %>
<%@ Import Namespace="System.Collections.Generic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script language="javascript" src="/js/jquery-1.4.2.min.js"></script>

    <script language="javascript" type="text/javascript">
        function addcontrol(pid,obj) {
            var ptid = $(obj).attr("pid")
            var s = $("input[pid='" + ptid + "']").size();
            var count = 0;
            $("input[pid='" + ptid + "']").each(function() {
                if (this.checked) {
                    count++;
                }
            });
            if (count == 0) {
                $("#chk" + ptid).attr("checked", false)
            }
            $.post("/manage/controlaction.aspx?act=addControl", { pid: pid }, function(data) {
                //alert(data);

                var da = data.split(':');
                da[0] = da[0].substring(0, da[0].length - 0);
                var ids1 = "";

                if (da[0] != null) {
                    ids1 = da[0].split(',');
                }
                $.each(ids1, function() {

                    $("#chk" + this).attr("checked", $('#chk' + pid).attr('checked'));

                })

                da[1] = da[1].substring(0, da[1].length - 0);
                var ids2 = "";


                if (da[1] != null) { da[1].split(','); }
                $.each(ids2, function() {
                    //alert($('#chk' + pid).attr('checked'));
                    if ($('#chk' + pid).attr('checked') == 1) {

                        $("#chk" + this).attr("checked", 1);

                    }
                    else {
                        borthers(pid);
                    }
                    //$(this).checked == $("#chk" + pid).checked
                })

                // var inputs = $("#chkDiv <input>");

            });
        }
        function borthers(pid) {
            $.post("/manage/controlaction.aspx?act=borthers", { pid: pid }, function(data) {
                //alert(data);
                if (data == "fail") {
                      
                 }
                else {
                    var da = data.split(':');
                    da[0] = da[0].substring(0, da[0].length - 1);
                    var da = data.split(',');
                    var flag = 0
                    $.each(da[0], function() {
                        if ($("chk" + this).attr("checked") == 1) {
                            flag = 1;
                        }

                    })
                    if (flag == 0) {
                        $("chk" + this).attr("checked", 0);
                        borthers(this);
                    }
                }
            });
        }

        $(function() {

            var pow = "<%=powers %>";

            var po = pow.split(',');
            var chklist = $("#chkDiv input");
            $.each(chklist, function() {
                $(this).attr("checked", 1);
            })
            $.each(po, function() {
                var p = this;
                $.each(chklist, function() {
                    //alert($(this).attr("id"));
                    //alert($("#" + $(this).attr("id")).val());
                    if ($("#" + $(this).attr("id")).val() == p) {
                        $("#" + $(this).attr("id")).attr("checked", 0);
                    }

                })

            })
        })

        function savecontrol(roleId) {
            var chklist = $("#chkDiv input");
            var pow = "";
            $.each(chklist, function() {

            if ($(this).attr("checked") == 0) { 
                    pow =pow+ $(this).val()+",";
                }
            })
            //alert(pow);
            $.post("/manage/controlaction.aspx?act=savecontrol", {roleId: roleId,pow:pow }, function(data) {
                //alert(data);
                if (data == "fail") {
                    alert("修改失败");
                }
                else {
                    alert("修改成功");
                }
            });
        }      
    </script>
</head>
<body>
    <form id="form1" runat="server" enctype="multipart/form-data">
    <div id="chkDiv">
        <%=GetControlTree(0) %>
    </div>
    <div>
    <input  type="button" value="保存权限" onclick="savecontrol(<%=roleId %>)"/>
      </div>  
    </form>
</body>
</html>
